package com.kerbores.wzcc;

import java.io.Serializable;
import java.util.List;
import java.util.Map;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.text.Html;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.BaseAdapter;
import android.widget.ListView;
import android.widget.TextView;

import com.kerbores.framworks.BaseActivity;

/**
 * 结果展示页
 * 
 * @author wangzhengfu
 * 
 */
public class ResultActivity extends BaseActivity {

	private ListView listView;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);

		setContentView(R.layout.activity_result);

		listView = (ListView) findViewById(R.id.listView);

		// 获取传递过来的数据
		List<Map<String, String>> result = (List<Map<String, String>>) getIntent().getSerializableExtra("result");

		ResultAdater adapter = new ResultAdater(context, result);
		listView.setAdapter(adapter);

		// 列表点击监听
		listView.setOnItemClickListener(new OnItemClickListener() {
			public void onItemClick(AdapterView<?> arg0, View arg1, int arg2, long arg3) {
				// 查看详细信息
				Map<String, String> imap = (Map<String, String>) arg0.getAdapter().getItem(arg2);

				Intent intent = new Intent(ResultActivity.this, ResultDetailActivity.class);
				intent.putExtra("imap", (Serializable) imap);
				startActivity(intent);
			}
		});
	}

	private class ResultAdater extends BaseAdapter {
		private List<Map<String, String>> list;

		@Override
		public int getCount() {
			// TODO Auto-generated method stub
			return list.size();
		}

		@Override
		public Map<String, String> getItem(int arg0) {
			// TODO Auto-generated method stub
			return list.get(arg0);
		}

		@Override
		public long getItemId(int arg0) {
			// TODO Auto-generated method stub
			return arg0;
		}

		@Override
		public View getView(int index, View view, ViewGroup arg2) {
			// TODO Auto-generated method stub
			ViewHolder holder = null;
			if (view == null) {
				view = getLayoutInflater().inflate(R.layout.result_item, null);

				holder = new ViewHolder();
				holder.tv_address = (TextView) view.findViewById(R.id.tv_address);
				holder.tv_fine = (TextView) view.findViewById(R.id.tv_fine);
				holder.tv_date = (TextView) view.findViewById(R.id.tv_date);

				view.setTag(holder);
			} else {
				holder = (ViewHolder) view.getTag();
			}

			Map<String, String> imap = getItem(index);

			holder.tv_address.setText(imap.get("area"));
			holder.tv_date.setText(imap.get("date"));

			StringBuilder sb = new StringBuilder();
			sb.append("<font color='#ef432b'>");
			sb.append("扣分");
			sb.append(imap.get("fen"));
			sb.append(" - ");
			sb.append("罚款");
			sb.append(imap.get("money"));
			sb.append("</font>");

			holder.tv_fine.setText(Html.fromHtml(sb.toString()));

			return view;
		}

		public ResultAdater(Context context, List<Map<String, String>> list) {
			super();
			this.list = list;
		}
	}

	private static class ViewHolder {
		TextView tv_address;// 地理位置
		TextView tv_fine;// 处罚信息
		TextView tv_date;// 处罚时间
	}

}
